<template>
  <div class="signup">
    <div class="signupbox">
      <div class="fbox">注册页面</div>

      <el-form class="inpbox" :model="signupform" :rules="formrules" ref="formref">
        <el-form-item label="用户名" prop="username">
          <el-input prefix-icon="iconfont icon-3702mima" v-model="signupform.username"></el-input>
        </el-form-item>

        <el-form-item label="密码" prop="password">
          <el-input
            prefix-icon="iconfont icon-users"
            v-model="signupform
          .password"
            type="password"
          ></el-input>
        </el-form-item>

        <el-form-item label="再次确认密码" prop="passwordSure">
          <el-input
            prefix-icon="iconfont icon-users"
            v-model="signupform
          .passwordSure"
            type="password"
          ></el-input>
        </el-form-item>

        <el-form-item class="btn">
          <el-button type="primary" plain @click="signup">注册</el-button>
          <!-- 重置表单功能 -->
          <!-- <el-button type="info" plain @click="formreset">重置</el-button> -->
          <el-button type="info" plain @click="toSignin">已有账号，去登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.signupform.password) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    var checkName = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入用户名"));
      } else if (value.length < 3 || value.length > 20) {
        callback(new Error("请输入长度在 3 到 20个字符!"));
      } else {
        this.checkNameOver(callback);
      }
    };
    return {
      signupform: {
        username: "",
        password: "",
        passwordSure: "",
      },

      formrules: {
        username: [
          //   { required: true, message: "请输入用户名", trigger: "blur" },
          //   { min: 3, max: 20, message: "长度在 3 到 20个字符", trigger: "blur" },
          { validator: checkName, trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 6, max: 20, message: "长度在 6 到 20个字符", trigger: "blur" },
        ],
        passwordSure: [{ validator: validatePass, trigger: "blur" }],
      },
    };
  },

  methods: {
    signup() {
      this.$refs.formref.validate(async (valid) => {
        if (!valid) return this.$message.error("用户名或密码不符合要求");
        const res = await this.$http.post("/users", this. signupform);
        console.log(res);
         if(res.data.state!==200){
           this.$message.error('添加用户失败！')
         }
       
        this.$message.success("添加成功！");
        this.$router.push("/login")
      });
    },
    toSignin() {
      this.$router.push("/login");
    },
    //验证名字是否被注册过
    async checkNameOver(callback) {
      if (this.signupform.username.length < 3) return;

      const res = await this.$http.get(
        "/checkname/" + this.signupform.username
      );
      if (res.data.state === 200) {
        callback();
      } else {
        callback(new Error("用户名被注册过了"));
      }
    },
  },
};
</script>

<style lang="less" scoped>
.signup {
  background-color: darkgray;
  height: 100%;

  .signupbox {
    width: 450px;
    height: 400px;
    background-color: whitesmoke;
    border-radius: 20px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    .fbox {
      text-align: center;
      font-size: 20px;
      font-weight: 900;
    }
  }
}
.inpbox {
  padding: 0 10px;
}
.btn {
  float: right;
  padding-right: 10px;
}
</style>